{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "#  issubclass和isinstance函数：检查类型\n",
    "\n",
    "Python 提供了如下两个函数来检查类型：\n",
    "> issubclass(cls, class_or_tuple)：检查 cls 是否为后一个类或元组包含的多个类中任意类的子类。\n",
    "> isinstance(obj, class_or_tuple)：检查 obj 是否为后一个类或元组包含的多个类中任意类的对象。\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\"Hello\"是否是str类的实例:  True\n",
      "\"Hello\"是否是object类的实例:  True\n",
      "str是否是object类的子类:  True\n",
      "\"Hello\"是否是tuple类的实例:  False\n",
      "str是否是tuple类的子类:  False\n",
      "[2,4]是否是list的实例:  True\n",
      "[2,4]是否是object类及其子类的实例:  True\n",
      "list是否是object类的子类:  True\n"
     ]
    }
   ],
   "source": [
    "# 定义一个字符串\n",
    "hello = 'Hello'\n",
    "# 'Hello' 是str类的实例\n",
    "print('\"Hello\"是否是str类的实例: ', isinstance(hello, str))\n",
    "# \"Hello\" 是object类的子类的实例\n",
    "print('\"Hello\"是否是object类的实例: ', isinstance(hello, object))\n",
    "# str是object类的子类\n",
    "print('str是否是object类的子类: ', issubclass(str, object))\n",
    "# \"Hello\"不是tuple类的实例\n",
    "print('\"Hello\"是否是tuple类的实例: ', isinstance(hello, tuple))\n",
    "# str不是tuple类的子类\n",
    "print('str是否是tuple类的子类: ', issubclass(str, tuple))\n",
    "\n",
    "# 定义一个列表\n",
    "my_list = [2, 4]\n",
    "# [2, 4]是list的实例\n",
    "print('[2,4]是否是list的实例: ', isinstance(my_list, list))\n",
    "# [2, 4]是object类的子类的实例\n",
    "print('[2,4]是否是object类及其子类的实例: ', isinstance(my_list, object))\n",
    "# list是object类的子类\n",
    "print('list是否是object类的子类: ', issubclass(list, object))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "issubclass() 和 isinstance() 两个函数的第二个参数都可使用元组, 表示或"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "data是否为列表或元组:  True\n",
      "str是不是list或tuple的子类:  False\n",
      "str是否为list或tuple或object的子类:  True\n"
     ]
    }
   ],
   "source": [
    "data = (20, 'fkit')\n",
    "print('data是否为列表或元组: ', isinstance(data, (list, tuple)))\n",
    "# str 不是list或tuple的子类\n",
    "print('str是不是list或tuple的子类: ', issubclass(str, (list, tuple)))\n",
    "# str是list或tuple或object的子类\n",
    "print('str是否为list或tuple或object的子类: ', issubclass(str, (list, tuple, object)))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "Python 为所有类都提供了一个 __bases__ 属性，通过该属性可以查看该类的所有直接父类，该属性返回所有直接父类组成的元组。"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "类A的所有父类:  (<class 'object'>,)\n",
      "类B的所有父类:  (<class 'object'>,)\n",
      "类C的所有父类:  (<class '__main__.A'>, <class '__main__.B'>)\n"
     ]
    }
   ],
   "source": [
    "class A:\n",
    "    pass\n",
    "class B:\n",
    "    pass\n",
    "class C(A, B):\n",
    "    pass\n",
    "\n",
    "print('类A的所有父类: ', A.__bases__)\n",
    "print('类B的所有父类: ', B.__bases__)\n",
    "print('类C的所有父类: ', C.__bases__)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "Python 还为所有类都提供了一个 __subclasses__() 方法，通过该方法可以查看该类的所有直接子类，该方法返回该类的所有子类组成的列表"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "类A的所有子类:  [<class '__main__.C'>]\n",
      "类C的所有子类:  []\n"
     ]
    }
   ],
   "source": [
    "print('类A的所有子类: ', A.__subclasses__())\n",
    "print('类C的所有子类: ', C.__subclasses__())"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}